package com.newly.center.business.service.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.newly.center.business.service.MQTTSerialService;
import com.newly.common.base.entity.R;
import com.newly.common.base.entity.business.Vo.SerialVo;
import com.newly.common.mapper.sys.DeviceMapper;
import com.newly.driver.mqtt.utils.MqttUtils;
import com.newly.driver.mqtt.utils.ReturnResult;
import com.newly.transfer.redis.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("mqttSerialService")
public class MQTTSerialServiceImpl implements MQTTSerialService {
    @Autowired
    RedisUtils redisUtils;
    @Autowired
    DeviceMapper deviceMapper;
    
    @Override
    public R getSerials(String productKey) {
        String token = redisUtils.get("mqtt"+productKey,String.class);
        String requestTopic = "/sys/device/"+ productKey+"/command/request/request-id=0x40000001";
        String responseTopic = "/sys/device/"+ productKey+"/command/response/request-id=0x40000001";

        JSONObject result = new JSONObject();
        result.put("version","1.0");
        result.put("id",productKey);
        result.put("token",token);

        String index= "{'index':null}";
        result.put("params",JSONUtil.parseObj(index));

        MqttUtils.publishMessage(requestTopic,result,1);
        R response = ReturnResult.getResult(responseTopic, redisUtils);
        response.setData(JSONUtil.parseObj(JSONUtil.toJsonStr(response.getData()).replace('-','_')));
        return response;
    }

    @Override
    public R getSerial(String productKey,int index) {
        String token = redisUtils.get("mqtt"+productKey,String.class);
        String requestTopic = "/sys/device/"+ productKey+"/command/request/request-id=0x40000002";
        String responseTopic = "/sys/device/"+ productKey+"/command/response/request-id=0x40000002";

        JSONObject result = new JSONObject();
        result.put("version","1.0");
        result.put("id",productKey);
        result.put("token",token);
        JSONObject params = new JSONObject();
        params.put("index",index);
        result.put("params",params);

        MqttUtils.publishMessage(requestTopic,result,1);
        return ReturnResult.getResult(responseTopic,redisUtils);
    }

    @Override
    public R setSerial(SerialVo serialVo) {
        String token = redisUtils.get("mqtt"+serialVo.getProductKey(),String.class);
        String requestTopic = "/sys/device/"+ serialVo.getProductKey()+"/command/request/request-id=0x40000003";
        String responseTopic = "/sys/device/"+ serialVo.getProductKey()+"/command/response/request-id=0x40000003";

        JSONObject result = new JSONObject();
        result.put("version","1.0");
        result.put("id",serialVo.getProductKey());
        result.put("token",token);
        JSONObject params = new JSONObject();
        JSONObject serial = new JSONObject();
        serial.put("index",serialVo.getIndex());
        JSONObject setting = new JSONObject();
        setting.put("baudrate",serialVo.getBaudrate());
        setting.put("databit",serialVo.getDatabit());
        setting.put("stopbit",serialVo.getStopbit());
        setting.put("parity",serialVo.getParity());
        setting.put("flow-control",serialVo.getFlowControl());
        serial.put("setting",setting);
        serial.put("connect",serialVo.getConnect());
        serial.put("protocol",serialVo.getProtocol());
        JSONObject overlay = new JSONObject();
        overlay.put("slot",serialVo.getSlot());
        JSONObject pos = new JSONObject();
        pos.put("x",serialVo.getX());
        pos.put("y",serialVo.getY());
        overlay.put("pos",pos);
        serial.put("overlay",overlay);

        params.put("serial",serial);
        result.put("params",params);

        MqttUtils.publishMessage(requestTopic,result,1);
        R response = ReturnResult.getResult(responseTopic, redisUtils);
//        if(response.getCode()==0){
//
//        }
        return response;
    }
}
